package com.hwd.gmall.payment.config;

import com.hwd.gmall.common.constant.PaymentConst;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 支付微服务的rabbit配置类
 *
 * @author 黄伟东/Victor
 * @date 2022/5/10 22:15
 */
@Configuration
public class PayRabbitConfig {

    /**
     * 创建订单支付交换机
     *
     * @return org.springframework.amqp.core.Exchange
     */
    @Bean("orderPayExchange")
    public Exchange orderPayExchange() {
        return ExchangeBuilder.directExchange(PaymentConst.ORDER_PAY_EXCHANGE).build();
    }

    /**
     * 创建普通订单的支付结果通知队列
     *
     * @return org.springframework.amqp.core.Queue
     */
    @Bean("orderPayQueue")
    public Queue orderPayQueue() {
        return QueueBuilder.durable(PaymentConst.ORDER_PAY_QUEUE).build();
    }


    /**
     * 创建秒杀订单的支付结果通知创建队列
     */
    @Bean("seckillOrderPayQueue")
    public Queue seckillOrderPayQueue() {
        return QueueBuilder.durable(PaymentConst.SECKILL_ORDER_PAY_QUEUE).build();
    }

    /**
     * 订单支付交换机和队列进行绑定
     *
     * @param orderPayExchange 订单支付交换机
     * @param orderPayQueue    订单支付队列
     * @return org.springframework.amqp.core.Binding
     */
    @Bean
    public Binding orderPayBinding(@Qualifier("orderPayExchange") Exchange orderPayExchange,
                                   @Qualifier("orderPayQueue") Queue orderPayQueue) {
        return BindingBuilder.bind(orderPayQueue)
                .to(orderPayExchange)
                .with(PaymentConst.ORDER_PAY_ROUTING_KEY)
                .noargs();
    }


    /**
     * 秒杀订单支付交换机和队列进行绑定
     *
     * @param orderPayExchange     订单支付交换机
     * @param seckillOrderPayQueue 秒杀订单支付队列
     * @return org.springframework.amqp.core.Binding
     */
    @Bean
    public Binding seckillOrderBinding(@Qualifier("orderPayExchange") Exchange orderPayExchange,
                                       @Qualifier("seckillOrderPayQueue") Queue seckillOrderPayQueue) {
        return BindingBuilder.bind(seckillOrderPayQueue)
                .to(orderPayExchange)
                .with(PaymentConst.SECKILL_ORDER_PAY_ROUTING_KEY)
                .noargs();
    }
}
